//
//  XLImageAndTitleButton.m
//  campus
//
//  Created by xl on 2019/11/15.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLImageAndTitleButton.h"


@interface XLImageAndTitleButton()

@property (nonatomic, assign) CGSize imageSize;
@property (nonatomic, assign) CGFloat space;
@property (nonatomic, assign) XLImageRelativeTitlePosition position;

@end

@implementation XLImageAndTitleButton

- (instancetype)initWithFrame:(CGRect)frame imageSize:(CGSize)imageSize imageAndTitleSpace:(CGFloat)imageAndTitleSpace imageRelativeTitlePosition:(XLImageRelativeTitlePosition)imageRelativeTitlePosition {

    self = [super initWithFrame:frame];
    if (self) {
        self.imageSize = imageSize;
        self.space = imageAndTitleSpace;
        self.position = imageRelativeTitlePosition;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGSize textSize = [self.titleLabel sizeThatFits:CGSizeZero];
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    switch (self.position) {
        case XLImageRelativeTitlePositionLeft: {  //图片在左文字在右
            CGFloat imageX = (width-self.space-self.imageSize.width-textSize.width)/2;
            CGFloat imageY = (height-self.imageSize.height)/2;
            self.imageView.frame = CGRectMake(imageX, imageY, self.imageSize.width, self.imageView.height);
            CGFloat labelX = imageX+self.imageSize.width+self.space;
            CGFloat labelY = (height-textSize.height)/2;
            self.titleLabel.frame = CGRectMake(labelX, labelY, textSize.width, textSize.height);
        }
            break;
        case XLImageRelativeTitlePositionRight: { //图片在右文字在左
            CGFloat labelX = (width-textSize.width-self.space-self.imageSize.width)/2;
            CGFloat labelY = (height-textSize.height)/2;
            self.titleLabel.frame = CGRectMake(labelX, labelY, textSize.width, textSize.height);
                        CGFloat imageX = labelX+self.space+textSize.width;
            CGFloat imageY = (height-self.imageSize.height)/2;
            self.imageView.frame = CGRectMake(imageX, imageY, self.imageSize.width, self.imageView.height);
        }
            break;
        case XLImageRelativeTitlePositionTop: { //图片在上文字在下
            CGFloat imageX = (width-self.imageSize.width)/2;
            CGFloat imageY = (height-self.imageSize.height-self.space-textSize.height)/2;
            self.imageView.frame = CGRectMake(imageX, imageY, self.imageSize.width, self.imageView.height);
            CGFloat labelX = (width-textSize.width)/2;
            CGFloat labelY = imageY+self.imageSize.height+self.space;
            self.titleLabel.frame = CGRectMake(labelX, labelY, textSize.width, textSize.height);
        }
            break;
        case XLImageRelativeTitlePositionBottom: { //图片在下文字在上
            CGFloat labelX = (width-textSize.width)/2;
            CGFloat labelY = (height-textSize.height-self.space-self.imageSize.height)/2;
            self.titleLabel.frame = CGRectMake(labelX, labelY, textSize.width, textSize.height);
            CGFloat imageX = (width-self.imageSize.width)/2;
            CGFloat imageY = labelY+self.imageSize.height+self.space;
            self.imageView.frame = CGRectMake(imageX, imageY, self.imageSize.width, self.imageView.height);
        }
            break;
            
        default:
            break;
    }
}

@end
